// 测试发布Agent功能的脚本
const http = require('http');

// 测试数据
const testData = {
  hotspot_title: "测试热点标题",
  hotspot_content: "这是测试热点内容，用于验证发布Agent功能。",
  platforms: {
    weibo: true,
    douyin: true,
    wechat: true
  },
  styles: ["专业严谨", "简洁明了"],
  platforms_to_publish: ["weibo", "douyin", "wechat"],
  publish_schedule: "immediate"
};

// 测试发布API
function testPublishAPI() {
  console.log('开始测试发布功能...');
  
  const options = {
    hostname: 'localhost',
    port: 5001,
    path: '/api/publish/execute',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(JSON.stringify(testData))
    }
  };

  const req = http.request(options, (res) => {
    let data = '';
    
    res.on('data', (chunk) => {
      data += chunk;
    });

    res.on('end', () => {
      try {
        const result = JSON.parse(data);
        console.log('\n发布API响应结果:');
        console.log(JSON.stringify(result, null, 2));
        
        if (result.success) {
          console.log('\n✅ 发布API调用成功！');
          console.log('\n发布结果摘要:');
          Object.keys(result.publish_results).forEach(platform => {
            const platformResult = result.publish_results[platform];
            console.log(`- ${platform}: ${platformResult.success ? '成功' : '失败'}`);
            if (platformResult.message) {
              console.log(`  消息: ${platformResult.message}`);
            }
          });
        } else {
          console.log('\n❌ 发布API调用失败！');
          console.log('错误信息:', result.message);
        }
      } catch (e) {
        console.error('\n❌ 解析响应失败:', e);
        console.log('原始响应:', data);
      }
    });
  });

  req.on('error', (e) => {
    console.error('\n❌ 请求发布API失败:', e.message);
  });

  req.write(JSON.stringify(testData));
  req.end();
}

// 测试发布测试API
function testPublishTestAPI() {
  console.log('开始测试发布测试功能...');
  
  const testTestData = {
    platforms_to_publish: ["weibo", "douyin", "wechat"]
  };
  
  const options = {
    hostname: 'localhost',
    port: 5001,
    path: '/api/publish/test',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(JSON.stringify(testTestData))
    }
  };

  const req = http.request(options, (res) => {
    let data = '';
    
    res.on('data', (chunk) => {
      data += chunk;
    });

    res.on('end', () => {
      try {
        const result = JSON.parse(data);
        console.log('\n发布测试API响应结果:');
        console.log(JSON.stringify(result, null, 2));
        
        if (result.success) {
          console.log('\n✅ 发布测试API调用成功！');
        } else {
          console.log('\n❌ 发布测试API调用失败！');
          console.log('错误信息:', result.message);
        }
      } catch (e) {
        console.error('\n❌ 解析响应失败:', e);
        console.log('原始响应:', data);
      }
    });
  });

  req.on('error', (e) => {
    console.error('\n❌ 请求发布测试API失败:', e.message);
  });

  req.write(JSON.stringify(testTestData));
  req.end();
}

// 运行测试
console.log('========================================');
console.log('OpenHotSpot 发布Agent功能测试');
console.log('========================================');

// 先测试测试API
setTimeout(() => {
  testPublishTestAPI();
  
  // 3秒后测试发布API
  setTimeout(() => {
    testPublishAPI();
  }, 3000);
}, 1000);